package com.system.healthproject.model.admin.controller;
import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.admin.paramsbean.*;
import com.system.healthproject.model.admin.service.AdminPatientService;
import com.system.healthproject.model.admin.service.AdminVisitRecordsService;
import com.system.healthproject.pojo.*;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

@Api(value = "后端患者管理",tags = "后端-患者管理API")
@RestController
@RequestMapping("/admin/patient")
@Validated
public class AdminPatientController {

    @Autowired
    AdminPatientService adminPatientService;
    @Autowired
    AdminVisitRecordsService adminVisitRecordsService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "name",value = "患者姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "sex",value = "性别",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "city",value = "居住区域",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "doctorId",value = "医生id",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "hasRecord",value = "是否填写病例1是0否",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "diagnosis_time",value = "上次就诊时间",paramType = "query",dataType = "String")

    })
    @ApiOperation(value = "后端获取所有患者列表(后端已调)",httpMethod = "get")
    @GetMapping(value = "/list",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<PatientParems>> getListPatient(
             @NotNull Integer page,
             @NotNull Integer size,
             @RequestParam(name = "name",required = false) String name,
             Integer sex,
             Integer city,
             Integer doctorId,
             Integer hasRecord,
             String  diagnosis_time
    ){
        PagesBean<PatientParems> pagesBean=
                adminPatientService.getListPatient(page,size,name,sex,city,hasRecord,diagnosis_time,doctorId);
        return ApiResult.successWithData("成功",pagesBean);
    }

    @ApiOperation(value = "后端根据患者id获取患者详情(后端已调)",httpMethod = "get")
    @GetMapping(value = "/patientInfoById",produces = "application/json;charset=utf-8")
    public ApiResult<PatientInfo> patientInfoById(
            @NotNull@RequestParam(value = "patientId",required = false) Integer patientId){
        PatientInfo patientInfo = adminPatientService.patientInfoById(patientId);
        return ApiResult.successWithData(patientInfo);
    }


    @ApiOperation(value = "后端患者地图获取患者数量(后端已调)",httpMethod = "get")
    @GetMapping(value = "/number",produces = "application/json;charset=utf-8")
    public ApiResult<PatientNumberParams> getPatientNumber(){
        PatientNumberParams patientNumber = adminPatientService.getPatientNumber();
        return ApiResult.successWithData("成功",patientNumber);
    }

    @ApiImplicitParam(name = "provinceCode",value = "省份识别码",paramType = "query",dataType = "string",required = true)
    @ApiOperation(value = "后端患者地图根据省份获取患者数量(后端已调)",httpMethod = "get")
    @GetMapping(value = "/numberByProvinceCode",produces = "application/json;charset=utf-8")
    public ApiResult<PatientNumberParams> getPatientNumberByProvinceCode(
            @NotNull(message = "省份识别码不能为空") Integer provinceCode){
        PatientNumberParams patientNumberParams = adminPatientService.adminPatientService(provinceCode);
        return ApiResult.successWithData("成功",patientNumberParams);
    }

    @ApiOperation(value = "后端患者地图获取省份列表并返回患者数量(后端已调)",httpMethod = "get")
    @GetMapping(value = "/privinceList",produces = "application/json;charset=utf-8")
    public ApiResult<List<ProvinceParams>> getIsPatientByProvince(){
        List<ProvinceParams> data=adminPatientService.getIsPatientByProvince();
        return ApiResult.successWithData("成功",data);
    }

    @ApiOperation(value = "更换用户的绑定的医生(后端已调)",httpMethod = "post")
    @PostMapping(value = "/changDoctor",produces = "application/json;charset=utf-8")
    public ApiResult changUserDoctors(
            @Validated
            @RequestBody PatientDoctor patientDoctor){
        boolean b = adminPatientService.changUserDoctors(patientDoctor);
        return ApiResult.withBoolean(b,"更换绑定医生成功","更换绑定医生失败");
    }

    @ApiOperation(value = "后台添加患者(后端已调)",httpMethod = "post")
    @PostMapping(value = "/addPatient",produces = "application/json;charset=utf-8")
    public ApiResult addPatient(
            @Validated
            @RequestBody
            AddPatientParams patientDoctor){
        boolean b = adminPatientService.addPatient(patientDoctor);
        return ApiResult.withBoolean(b,"添加用户成功","添加用户失败");
    }

    @ApiOperation(value = "后台根据患者id修改患者信息(后端已调)",httpMethod = "post")
    @PostMapping(value = "/editPatientById",produces = "application/json;charset=utf-8")
    public ApiResult editPatientById(
            @Validated
            @RequestBody EditPatientParams patientDoctor){
        boolean b = adminPatientService.editPatientById(patientDoctor);
        return ApiResult.withBoolean(b,"修改成功","修改失败");
    }

    @ApiOperation(value = "后台添加患者病例(后端已调)",httpMethod = "post")
    @PostMapping(value = "/addRecord",produces = "application/json;charset=utf-8")
    public ApiResult addRecord(
            @Validated
            @RequestBody AddRecordParams addRecordParams){
        ApiResult apiResult = adminPatientService.addRecord(addRecordParams);
        return apiResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true),
    })
    @ApiOperation(value = "根据用户id获取用户病例详情(后端已调)",httpMethod = "get")
    @GetMapping(value = "/getRecordInfoByUserId",produces = "application/json;charset=utf-8")
    public ApiResult<RecordInfoParams> getRecordInfoByUserId(
            @NotNull @RequestParam(value = "userId",required = false) Integer userId){
        RecordInfoParams recordInfoByUserId = adminPatientService.getRecordInfoByUserId(userId);
        return ApiResult.successWithData("成功",recordInfoByUserId);
    }

    @ApiOperation(value = "新增就诊记录(后端已调)",httpMethod = "post")
    @PostMapping(value = "/addVisitRecords",produces = "application/json;charset=utf-8")
    public ApiResult addVisitRecords(
            @Validated
            @RequestBody VisitRecordsParams2 addRecordParams){

        boolean b = adminVisitRecordsService.addVisitRecords(addRecordParams);
        return ApiResult.withBoolean(b,"新增就诊记录成功","新增就诊记录失败");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",paramType = "query",dataType = "int",required = true)
    })
    @ApiOperation(value = "获取患者的个人就诊记录列表(后端已调)",httpMethod = "get")
    @GetMapping(value = "/visitRecordsList",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<VisitRecordsShowParams>> getVisitRecordsList(
            @NotNull Integer page,
            @NotNull Integer size,
            @NotNull @RequestParam(value = "userId" ,required = false) Integer userId
    ){
        PagesBean<VisitRecordsShowParams> visitRecordsList
                =
                adminVisitRecordsService.getVisitRecordsList(page, size, userId);
        return ApiResult.successWithData("成功",visitRecordsList);
    }

    @ApiOperation(value = "获取患者的个人就诊记录详情(后端已调)",httpMethod = "get")
    @GetMapping(value = "/visitRecordsInfo",produces = "application/json;charset=utf-8")
    public ApiResult<VisitRecords> visitRecordsInfo(
            @NotNull @RequestParam(value = "patientId" ,required = false) Integer patientId
    ){
        VisitRecords visitRecordsInfo = adminVisitRecordsService.getVisitRecordsInfo(patientId);
        return ApiResult.successWithData("成功",visitRecordsInfo);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "统计类型(1.日,2.周,3.月)",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",paramType = "query",dataType = "int",required = true),
    })
    @ApiOperation(value = "按照日周月返回患者的发作次数(后端已调)",httpMethod = "get")
    @GetMapping(value = "/getFrequencyList",produces = "application/json;charset=utf-8")
    public ApiResult<List<FrequencyParams>> getFrequencyList(
            @NotNull Integer type,
            @NotNull Integer userId
    ){
        List<FrequencyParams> frequencyList = adminVisitRecordsService.getFrequencyList(type, userId);
        return ApiResult.successWithData(frequencyList);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "统计类型(1.日,2.周,3.月)",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",paramType = "query",dataType = "int",required = true),

    })
    @ApiOperation(value = "按照日周月返回患者的症状记录(后端已调)",httpMethod = "get")
    @GetMapping(value = "/getReactions",produces = "application/json;charset=utf-8")
    public ApiResult<List<ReactionsParams> > getReactions(
            @NotNull Integer type,
            @NotNull Integer userId){
        List<ReactionsParams> reactions = adminVisitRecordsService.getReactions(type, userId);
        return ApiResult.successWithData("成功",reactions);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "userId",value = "患者id",paramType = "query",dataType = "int",required = true)
    })
    @ApiOperation(value = "获取根据患者id病情日志列表(后端已调)",httpMethod = "get")
    @GetMapping(value = "/getDiseaseLogList",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<Map<String, Object>>> getDiseaseLogList(
            @NotNull Integer page,
            @NotNull Integer size,
            @NotNull Integer userId
    ){
        PagesBean<Map<String, Object>> diseaseLogList =
                adminVisitRecordsService.getDiseaseLogList(page, size, userId);
        return ApiResult.successWithData("成功",diseaseLogList);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",
                    value = "病情日志id",paramType = "query",dataType = "int"),
    })
    @ApiOperation(value = "根据id获取病情日志详情(后端已调)",httpMethod = "get")
    @GetMapping(value = "/getDiseaseLogById",produces = "application/json;charset=utf-8")
    public ApiResult<DiseaseLog> getDiseaseLogById(
            @NotNull Integer id){
        DiseaseLog diseaseLogById = adminVisitRecordsService.getDiseaseLogById(id);
        return ApiResult.successWithData("成功", diseaseLogById);
    }


}
